package Application.File;

import Application.AppJoint;
import javafx.scene.Parent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import Application.LayerImage.LayerImage;

import java.io.File;

public class FileAdd {
    public static File fileChoose(Stage primaryStage){
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("选择一张图片");
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("All images (.jpg .png .gif .bmp)","*.jpg","*.png","*.gif","*.bmp"),
                new FileChooser.ExtensionFilter("JPG","*.jpg (.jpg)"),
                new FileChooser.ExtensionFilter("PNG","*.png (.png)"),
                new FileChooser.ExtensionFilter("GIF","*.gif (.gif)"),
                new FileChooser.ExtensionFilter("BMP","*.bmp (.bmp)")
        );
        File image = fileChooser.showOpenDialog(primaryStage);
        //System.out.println(image);
        return image;
    }

    public static WritableImage imgAdd (File file){
        if(file == null){
            return null;
        }
        WritableImage writableImage;
        try{
            String localUrl = file.toURI().toURL().toString();
            //System.out.println(localUrl);
            Image image = new Image(localUrl);
            writableImage = new WritableImage((int)image.getWidth(),(int)image.getHeight());

            for(int i=0;i<(int)image.getWidth();i++) {
                for (int j = 0; j < (int) image.getHeight(); j++) {
                    writableImage.getPixelWriter().setArgb(i, j, image.getPixelReader().getArgb(i, j));
                    //System.out.println(image.getPixelReader().getArgb(i, j));
                }
            }
            return writableImage;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static LayerImage getLayerImageFromFile(File file) {
        if(file == null) {
            return null;
        }

        WritableImage wImage = FileAdd.imgAdd(file);

        LayerImage layer = new LayerImage((int)wImage.getWidth(), (int)wImage.getHeight(), wImage);
        return layer;
    }

    public static LayerImage loadImage(String path) {
//        File file = new File(path);
//
//        WritableImage img = FileAdd.imgAdd(file);
//        LayerImage layer = new LayerImage((int)img.getWidth(), (int)img.getHeight(), img);
//
//        return layer;


        //Image image = new Image("file:../../assets/masks/test.png");

        //System.out.println(path);
        File file = new File(path);
        if(!file.exists()) {
            System.out.println("aaa");
            System.out.println(file.getAbsolutePath());
            //return null;
        } else {
            System.out.println("bbb");
            System.out.println(file.getAbsolutePath());
        }
        WritableImage wImage = FileAdd.imgAdd(file);
        //WritableImage wImage = FileAdd.imgAdd(new File("Asset));

        LayerImage layer = new LayerImage((int)wImage.getWidth(), (int)wImage.getHeight(), wImage);
        //AppJoint.getInstance().getImageView().setImage(image);
        return layer;
    }

    public static File[] getFilesByPath(String path) {
        File file = new File(path);

        if(file.exists()) {
            File[] files = file.listFiles();

            return files;
        } else {
            return null;
        }
    }
}
